package com.changpay.zywy.ui.activity.loan

import android.content.Intent
import android.view.View
import com.ruffian.library.widget.RCheckBox
import com.changpay.zywy.R
import com.changpay.zywy.base.BaseActivity
import com.changpay.zywy.base.IView
import com.changpay.zywy.ext.showToast
import com.changpay.zywy.ext.sss
import com.changpay.zywy.http.RetrofitHelper
import kotlinx.android.synthetic.main.activity_loan_pay.*
import kotlinx.android.synthetic.main.title_layout.*
import java.lang.StringBuilder

class LoanPayActivity : BaseActivity(), IView {

    private var money: Double = 0.0
    val checkBoxList = arrayListOf<RCheckBox>()
    var stringBuilder: StringBuilder = StringBuilder()
    override fun showLoading() {

    }

    override fun hideLoading() {

    }

    override fun showDefaultMsg(msg: String) {
        showToast(msg)
    }

    override fun showMsg(msg: String) {
        showToast(msg)
    }

    override fun showError(errorMsg: String) {
        showToast(errorMsg)
    }


    private val onClickListener = View.OnClickListener { view ->

        when (view.id) {
            R.id.iv_back -> finish()
            R.id.tv_submit -> submit()
            R.id.number_one -> takePlay("1")
            R.id.number_two -> takePlay("2")
            R.id.number_three -> takePlay("3")
            R.id.number_four -> takePlay("4")
            R.id.number_five -> takePlay("5")
            R.id.number_six -> takePlay("6")
            R.id.number_seven -> takePlay("7")
            R.id.number_eight -> takePlay("8")
            R.id.number_nine -> takePlay("9")
            R.id.number_zero -> takePlay("0")
            R.id.number_sure -> takePlay("sure")
            R.id.number_back -> takePlay("back")
        }

    }

    private fun submit() {
        val params = HashMap<String, String>()
        params["key"] = token
        params["client"] = "android"
        RetrofitHelper.service.feedback_add(params).sss(this)
        {
            showToast(it.result.toString())
            finish()
        }
    }

    override fun attachLayoutRes(): Int {
        return R.layout.activity_loan_pay
    }

    override fun initData() {

    }

    override fun initView() {
        tv_title.text = "贷货管理"

        tv_jiezhang_fangshi.text = intent.getStringExtra("jiesuan_message")
         money = intent.getStringExtra("money").toDouble()
        tv_monye.text = "￥" + intent.getStringExtra("money")
        tv_message.text = "本期需"+intent.getStringExtra("type")+"总金额为" + intent.getStringExtra("money")

        tv_pay_message.text = "请输入支付密码\n确认" + intent.getStringExtra("type")

        iv_back.setOnClickListener(onClickListener)



        number_one.setOnClickListener(onClickListener)
        number_two.setOnClickListener(onClickListener)
        number_three.setOnClickListener(onClickListener)
        number_four.setOnClickListener(onClickListener)
        number_five.setOnClickListener(onClickListener)
        number_six.setOnClickListener(onClickListener)
        number_seven.setOnClickListener(onClickListener)
        number_eight.setOnClickListener(onClickListener)
        number_nine.setOnClickListener(onClickListener)
        number_zero.setOnClickListener(onClickListener)
        number_sure.setOnClickListener(onClickListener)
        number_back.setOnClickListener(onClickListener)
        checkBoxList.add(cb_one)
        checkBoxList.add(cb_two)
        checkBoxList.add(cb_three)
        checkBoxList.add(cb_four)
        checkBoxList.add(cb_five)
        checkBoxList.add(cb_six)
    }


    private fun takePlay(type: String) {
        when (type) {
            "back" -> {
                if (stringBuilder.isEmpty()) return
                stringBuilder.delete(stringBuilder.length - 1, stringBuilder.length)
                checkBoxList[stringBuilder.length].isChecked = false
            }
            "sure" -> {
                if (stringBuilder.length < 6) {
                    showToast("长度不足6位")
                    return
                }
                sure(stringBuilder.toString())
            }
            else -> {
                if (stringBuilder.length >= 6) return
                stringBuilder.append(type)
                checkBoxList[stringBuilder.length - 1].isChecked = true
            }
        }

    }


    private fun sure(password: String) {
        val params = HashMap<String, String>()
        params["key"] = token
        params["password"] = password
        RetrofitHelper.service.check_pd_pwd(params).sss(this)
        {
            params["allot_id"] = intent.getStringExtra("allot_id")
            params["code"] = intent.getStringExtra("code")
            RetrofitHelper.service.bill_refund(params).sss(this)
            {
                Intent(this, LoanPaySuccessActivity::class.java).run {
                    putExtra("type", intent.getStringExtra("type"))
                    putExtra("message",it.result.toString())
                    putExtra("allot_id",intent.getStringExtra("allot_id"))
                    startActivity(this)
                }
            }
        }
    }

    override fun start() {

    }
}
